package com.antgroup.seckill.business.goods.service.impl;

import com.antgroup.seckill.business.common.enums.RespBeanEnum;
import com.antgroup.seckill.business.common.exception.GlobalException;
import com.antgroup.seckill.business.common.vo.response.GoodsVo;
import com.antgroup.seckill.business.common.vo.response.OrderDetailVo;
import com.antgroup.seckill.business.common.vo.response.RespBean;
import com.antgroup.seckill.business.goods.mapper.OrderMapper;
import com.antgroup.seckill.business.goods.mapper.SeckillGoodsMapper;
import com.antgroup.seckill.business.goods.mapper.SeckillOrderMapper;
import com.antgroup.seckill.business.goods.model.Order;
import com.antgroup.seckill.business.goods.model.SeckillGoods;
import com.antgroup.seckill.business.goods.model.SeckillOrder;
import com.antgroup.seckill.business.goods.model.User;
import com.antgroup.seckill.business.goods.service.IGoodsService;
import com.antgroup.seckill.business.goods.service.IOrderService;
import com.antgroup.seckill.business.goods.service.ISeckillGoodsService;
import com.antgroup.seckill.business.utils.UUIDUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangxy
 * @since 2024-07-30
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private SeckillOrderMapper seckillOrderMapper;


    @Autowired
    private IGoodsService iGoodsService;

    @Autowired
    private ISeckillGoodsService seckillGoodsService;


    @Autowired
    private RedisTemplate redisTemplate;

    @Transactional
    @Override
    public Order seckillGoods(GoodsVo goodsVo, User user) {

        SeckillGoods seckillGoods = seckillGoodsMapper.selectOne(new QueryWrapper<SeckillGoods>().eq("goods_id", goodsVo.getId()));

        ValueOperations valueOperations = redisTemplate.opsForValue();
        //seckillGoods.setSeckillCount(seckillGoods.getSeckillCount() -1);

        //用mapper或者service都可以,扣DB库存
        boolean flag = seckillGoodsService.update(new UpdateWrapper<SeckillGoods>().setSql("seckill_count = " + "seckill_count-1").
                eq("goods_id", goodsVo.getId()).gt("seckill_count", 0));

        if(seckillGoods.getSeckillCount() < 1 ) {
            valueOperations.set("isStockEmpty:" + goodsVo.getId(),0);
            return null;
        }
        //生成订单表
        Order order = new Order();
        order.setId(UUIDUtil.uuid());
        order.setUserId(user.getId());
        order.setGoodsId(goodsVo.getId());
        order.setDeliveryAddrId(String.valueOf(0));
        order.setGoodsName(goodsVo.getGoodsName());
        order.setGoodsCount(1);
        order.setGoodsPrice(seckillGoods.getSeckillPrice());
        order.setOrderChannel(0);
        order.setStatus(String.valueOf(0));
        order.setModifyTime(new Date());
        order.setGmtTime(new Date());
        orderMapper.insert(order);

        //生成秒杀订单表
        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setId(UUIDUtil.uuid());
        seckillOrder.setUserId(user.getId());
        seckillOrder.setOrderId(order.getId());
        seckillOrder.setGoodsId(goodsVo.getId());
        seckillOrder.setGmtTime(new Date());
        seckillOrder.setModifyTime(new Date());
        seckillOrderMapper.insert(seckillOrder);
        redisTemplate.opsForValue().set("order:" + user.getId() + ":" + goodsVo.getId(),seckillOrder,10, TimeUnit.MINUTES);
        return order;
    }

    @Override
    public OrderDetailVo queryOrderDetailByOrderId(String orderId) {

        if(StringUtils.isEmpty(orderId)) {
            throw new GlobalException(RespBeanEnum.ORDER_NOT_EXIST);
        }

        Order order = orderMapper.selectById(orderId);

        GoodsVo goodsVo = iGoodsService.queryGoodsByGoodsId(order.getGoodsId());
        OrderDetailVo vo = new OrderDetailVo();
        vo.setOrder(order);
        vo.setGoodsVo(goodsVo);
        return vo;
    }


}
